﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ca2simulator.client.view.grid;
using ca2simulator.shared;
using ca2simulator.client.model.transfer;
using System.IO;
using ca2simulator.shared.Examples;
using Microsoft.Win32;

namespace ca2simulator.client.view
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string SIM_NAME = "LifeSimulation";
        private CA2Raw _Raw;
        

        public MainWindow()
        {
            InitializeComponent();
            Reset();
        }

        void Reset()
        {
            textBox_simName.Text = SIM_NAME;
            LifeExample life = new LifeExample();
            slider_numState.Value = LifeExample.NUMSTATE;
            slider_defaultState.Value = LifeExample.DEFAULTSTATE;
            richTextBox_code.Document.Blocks.Clear();
            richTextBox_code.AppendText(life.GetTransferCode());
        }

        void AddViewUserControl()
        {
            //TODO load init state from UI
            //LifeExample LifeExample = new LifeExample();

            //Lattice lat = LifeExample.GetInitialLattice("glider");

            //TODO load code from UI
            //string transferCode = LifeExample.GetTransferCode();
            CA2Raw raw;// = new CA2Raw(lat, transferCode);
            if (!CA2Factory.BuildCA2Raw("simulation_life.sim", out raw))
                throw new ArgumentException("simulation file could not be parsed");
            CA2 ca;
            if (CA2Factory.BuildCA2(raw, out ca))
            {
                stackPanel.Children.Add(new CAUserControl(raw));
            }
            else
            {
                Console.WriteLine("The code would not compile");
            }
                
            
            
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            AddViewUserControl();
        }

        private void slider_numState_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if(label_numState!=null) label_numState.Content = (int)slider_numState.Value;
            if(slider_defaultState!=null) slider_defaultState.Maximum = (int)slider_numState.Value-1;
        }

        private void slider_defaultState_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if(label_defaultState!=null) label_defaultState.Content = (int)slider_defaultState.Value;
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.DefaultExt = "sim";
            file.Filter = "Simulation Files (.sim)|*.sim";
            Nullable<bool> result = file.ShowDialog();
            CA2Raw raw;
            if (result == false) return;
            if (!CA2Factory.BuildCA2Raw(file.FileName, out raw))
            {
                MessageBox.Show("Unable to open simulation file, follow error occured.\n" + CA2Factory.ERRNO);
            }
            else
            {
                _Raw = raw;
                UpdateUiRaw();
            }
        }

        private void UpdateUiRaw()
        {
            richTextBox_code.Document.Blocks.Clear();
            richTextBox_code.AppendText(_Raw.TransferCode);
            slider_defaultState.Value = _Raw.InitialLattice.InitialState.State;
            slider_numState.Value = 256;

        }
        
    }

   
}
